<?php
class Tour extends TourPeer
{
	function Tour()
	{
		parent::TourPeer();
	}
	
	public static function getAllTours($active=1, $orderby='id', $limit=NULL)
	{
		$database = new Database();
		if ($active != 1)
			$active = 0;
		$query = "SELECT id, start_date, tour_type FROM Tour WHERE active = $active ORDER BY $orderby ";
		$result = $database->query($query);
		$ids = $database->fetch_array($result);
		$tours = array();
		foreach ($ids as $id) {
			if (($active == 1) && ($id['tour_type'] == 'NORMAL') && strtotime($id['start_date']) < time())
			{
				$tour_expire = new Tour();
				$tour_expire->select($id['id']);
				$tour_expire->setactive(0);
				$tour_expire->update();
			}
			else {
				$t = new Tour();
				$t->select($id['id']);
				$tours[] = $t;
			}
		}
		return $tours;
	}
	
	function getRealPrice()
	{
		$price = $this->getprice();
		$promotion = $this->getpromotion();
		if ($promotion != 0)
			$price = $price * (100-$promotion) / 100;
		return $price;
	}
	
	function getCurrencyObj()
	{
		$currency_id = $this->getcurrency();
		$currency = new Currency();
		$currency->select($currency_id);
		return $currency;
	}
	
	function getAllPictures()
	{
		$picture_ids = $this->getpictures();
		$picture_ids = json_decode($picture_ids, true);
		$pictures = array();
		if ($picture_ids)
			foreach ($picture_ids as $id) 
				if ($id) {
				$pic = new Picture();
				$pic->select($id);
				$pictures[] = $pic;
			}
		
		return $pictures;
	}
	
	
	function getAllLocations()
	{
		$db = new DataBase();
		$sql = "SELECT location_id FROM tour_location WHERE tour_id = ".$this->getid()." ORDER BY location_index asc;";
		$result = $db->query($sql);
		$ids = $db->fetch_array($result);
		$locations = array();
		foreach ($ids as $id) {
			$location = new Location();
			$location->select($id['location_id']);
			$locations[] = $location;
		}
		return $locations;
	}
	
	function getAllTourCategories()
	{
		$database = new Database();
		$query = "SELECT id FROM tour_category WHERE tour_id = '".$this->getid()."'";
		$result = $database->query($query);
		$ids = $database->fetch_array($result);
		$categories = array();
		foreach ($ids as $id) {
			$t = new TourCategory();
			$t->select($id['id']);
			$categories[] = $t;
		}
		return $categories;
	}
	
	function getAllAvailableLocations()
	{
		$locations = Location::getAllLocations();
		$added_locations = $this->getAllLocations();
		
		$location_ids = array();
		foreach ($locations as $location)
			$location_ids[] = $location->getid();
			
		$added_location_ids = array();
		foreach ($added_locations as $added_location)
			$added_location_ids[] = $added_location->getid();
		
		$ids = array();
		foreach ($location_ids as $id) {
			if (!in_array($id, $added_location_ids))
				$ids[] = $id;
		}
		
		$locations = array();
		foreach ($ids as $id) {
			$location = new Location();
			$location->select($id);
			$locations[] = $location;
		}
		return $locations;
	}
	
	function getDeparture()
	{
		$db = new Database();
		$query = "SELECT location_id FROM tour_location WHERE tour_id = '".$this->getid()."' AND location_index = 1";
		$db->query($query);
		if ($result = mysql_fetch_row($db->result))
		{
			$id = $result[0];
			$location = new Location();
			$location->select($id);
			return $location;
		}
		return false;
	}
	
	function getDestination()
	{
		$db = new Database();
		$current_index = TourLocation::getCurrentIndex($this->getid()) + 0;
		$query = "SELECT location_id FROM tour_location where tour_id = ".$this->getid()." AND location_index = ".$current_index;
		$db->query($query);
		if ($result = mysql_fetch_row($db->result))
		{
			$id = $result[0];
			$location = new Location();
			$location->select($id);
			return $location;
		}
		return false;
	}
	
	public static function customizeQuery($query)
	{
		$db = new Database();
		$db->query($query);
		$result = $db->result;
		$ids = $db->fetch_array($result);
		$tours = array();
		foreach ($ids as $id) {
			$t = new Tour();
			$t->select($id['id']);
			$tours[] = $t;
		}
		return $tours;
	}
	
	function isInCategory($id, $name = '')
	{
		$db = new Database();
		if ($name != '') { // searching by name
			$query = "SELECT  tour_category.tour_id FROM tour_category LEFT JOIN category ON category.id WHERE category.name = ".$name." AND tour_category.tour_id = ".$this->getid();
		} else {
			$query = "SELECT  tour_category.tour_id FROM tour_category LEFT JOIN category ON category.id WHERE category.id = ".$id." AND tour_category.tour_id = ".$this->getid();
		}
		$db->query($query);
		return $db->result? true : false;
		
	}
}
?>